import { createRouter, createWebHistory } from 'vue-router'
// import HomeView from '../views/HomeView.vue'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    //登录
    {
      path:'/',
      name:"login",
      component:()=>import("../views/Login.vue")
    },
    // 首页
    {
      path:"/mainbox",
      name:"mainbox",
      component:()=>import("../views/MainBox.vue"),
      // children:[
      //   {
      //     path:"/a"
      //   }
      // ]
    }
  ]
})
//个人中心
router.addRoute("mainbox",{
     path:'/center',
     component:()=>import("../views/center/Center.vue")
})
//首页
router.addRoute("mainbox",{
     path:'/index',
     component:()=>import("../views/home/Home.vue")
})
//添加用户
router.addRoute("mainbox",{
  path:'/useradd',
  component:()=>import("../views/user-manage/userAdds.vue")
})
//用户列表
router.addRoute("mainbox",{
  path:'/userlist',
  component:()=>import("../views/user-manage/userList.vue")
})
// 添加新闻
router.addRoute("mainbox",{
  path:'/newsadd',
  component:()=>import("../views/news-manage/newsAdd.vue")
})
// 新闻列表
router.addRoute("mainbox",{
  path:'/newslist',
  component:()=>import("../views/news-manage/newsList.vue")
})
// 添加产品
router.addRoute("mainbox",{
  path:'/productadd',
  component:()=>import("../views/product-manage/productAdd.vue")
})
// 产品列表
router.addRoute("mainbox",{
  path:'/productlist',
  component:()=>import("../views/product-manage/productList.vue")
})
router.beforeEach((to,from,next)=>{
  if(to.name==="login"){
    next()
  }else{
    if(!localStorage.getItem("token")){
      next({
        path:"/login"
      })
    }else{
      next()
    }
  }
})
export default router
